<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/修改装备品阶</title>
<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" id="saveAndUpdateFrom" target="_parent" action="">
			<input type="hidden" id="rowId" name="rowId" value='<%=request.getParameter("rowId")%>'/>
			<input type="hidden" id="isReCommit" value="0">
			<input type="hidden" id="token" name="token" value="">
			<input type="hidden" id="gameId" name="gameId" value="">
			<input type="hidden" id="gameName" name="gameName" value="">
			<div class="form-group">
				<label class="col-xs-3 text-right">游戏名称:</label>
				<div class="col-xs-5">
					<sk:select cssClass="form-control select" id="gameIdSelect" name="gameIdSelect" value="" requiredInit="true" ajaxURL="${userInfoV1}/baseGame/gameSelect?wm[DEL_FLAG]=1">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">装备品阶名称:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="equipQuality" id="equipQuality" value=""  placeholder="请输入装备品阶名称" onblur="checkGameEquipLevel('');">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">最低前缀数量:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="lowPrefixNum" id="lowPrefixNum" value=""  placeholder="请输入最低前缀数量">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">最高前缀数量:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="highPrefixNum" id="highPrefixNum" value=""  placeholder="请输入最高前缀数量">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">最低后缀数量:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="lowSuffixNum" id="lowSuffixNum" value=""  placeholder="请输入最低后缀数量">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">最高后缀数量:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="highSuffixNum" id="highSuffixNum" value=""  placeholder="请输入最高后缀数量">
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-3 text-right">是否开启:</label>
				<div class="col-xs-7">
					<td colspan="2">
						<label><input name="openFlag" type="radio" checked="checked"  value="1"/>&nbsp;是</label>&nbsp;&nbsp;&nbsp;&nbsp;
						<label><input name="openFlag" type="radio"  value="0"/>&nbsp;否</label>
					</td>
				</div>
			</div>
		</sk:form>
	</div>
</body>
<%@ include file="/view/common/footer.jsp"%>
<script>
    var gameEquipLevelId = $("#rowId").val();//装备品阶ID
    $(function(){
        //添加修改装备品阶时根据ID控制
        if (CheckUtil.isNotNull(gameEquipLevelId) && parseInt(gameEquipLevelId) > 0) {
            $('#gameIdSelect').attr("disabled", "true");
        }
        //后台登录用户token
        $("#token").val(sessionStorage.getItem("token"));
        //修改装备品阶时获取数据
        if(gameEquipLevelId > 0){
            $.ajax({
                url: "${userInfoV1}/gameEquipLevel/getGameEquipLevelInfo",
                type: "post",
                cache: false,
                data:{"rowId":gameEquipLevelId},
                dataType:"json",
                async:false,
                success: function(data) {
                    if (data == null) {
                        return;
                    }
                    $("#gameId").val(data.gameId);
                    $("#gameName").val(data.gameName);
                    $("#gameIdSelect").val(data.gameId);
                    $("#equipQuality").val(data.equipQuality);
                    $("#lowPrefixNum").val(data.lowPrefixNum);
                    $("#highPrefixNum").val(data.highPrefixNum);
                    $("#lowSuffixNum").val(data.lowSuffixNum);
                    $("#highSuffixNum").val(data.highSuffixNum);
                    $("input[name='openFlag']").each(function(){
                        if (data.openFlag == $(this).val()) {
                            $(this).attr("checked",true);
                        } else {
                            $(this).attr("checked",false);
                        }
                    });
                }
            });
        }
    });
    //校验装备品阶名称是否重复
    function checkGameEquipLevel(operaType) {
        var equipQuality = $("#equipQuality").val();
        var gameId = $("#gameIdSelect").val();
        var rowId = $("#rowId").val();
        if (!CheckUtil.isNotNull(gameId)) {
            layer.msg("请选择游戏!");
            return;
        }
        if (!CheckUtil.isNotNull(equipQuality)) {
            layer.msg("请输入装备品阶名称!");
            return;
        }
        if (CheckUtil.isNotNull(equipQuality) && CheckUtil.isNotNull(gameId)) {
            $.ajax({
                type: "post",
                cache: false,
                url: "${userInfoV1}/gameEquipLevel/checkGameEquipLevel",
                data:{equipQuality:equipQuality, rowId:rowId, gameId:gameId},
                dataType:"json",
                async:false,
                success: function(data) {
                    result = data;
                    if (data == true || data == "true") {
                        layer.msg("装备品阶名称重复");
                        $("#raiderType").focus();
                    } else if (operaType == "1") {
                        addAfterCheck();
                    }
                }
            });
        }
    }
    var err = {'1':'添加','2':'修改'};
    var action = {'1': '${userInfoV1}/gameEquipLevel/save','2': '${userInfoV1}/gameEquipLevel/update'};
    function add(){
        //检查装备品阶名称是否重复
        checkGameEquipLevel("1");
    }
    //装备品阶名称不重复则执行
    function addAfterCheck() {
        var rowId = $('#rowId').val();
        var opera;
        if (!CheckUtil.isNotNull(rowId) || rowId <= 0) {
            opera = 1;//添加
        } else {
            opera = 2;//修改
        }
        var gameId = $("#gameIdSelect").val();
        if (!CheckUtil.isNotNull(gameId)) {
            layer.msg("请选择游戏!");
            return;
        }
        var equipQuality = $("#equipQuality").val();
        if (!CheckUtil.isNotNull(equipQuality)) {
            layer.msg("请输入装备品阶名称!");
            return;
        }
        var lowPrefixNum = $("#lowPrefixNum").val();
        var highPrefixNum = $("#highPrefixNum").val();
        var lowSuffixNum = $("#lowSuffixNum").val();
        var highSuffixNum = $("#highSuffixNum").val();
        if (!CheckUtil.isNotNull(lowPrefixNum) || !CheckUtil.isInt(lowPrefixNum, "+")) {
            layer.msg("请输入正确的最低前缀数量!");
            return;
        }
        if (!CheckUtil.isNotNull(highPrefixNum) || !CheckUtil.isInt(highPrefixNum, "+")) {
            layer.msg("请输入正确的最高前缀数量!");
            return;
        }
        if (!CheckUtil.isNotNull(lowSuffixNum) || !CheckUtil.isInt(lowSuffixNum, "+")) {
            layer.msg("请输入正确的最低后缀数量!");
            return;
        }
        if (!CheckUtil.isNotNull(highSuffixNum) || !CheckUtil.isInt(highSuffixNum, "+")) {
            layer.msg("请输入正确的最高后缀数量!");
            return;
        }
        var openFlag =$('input:radio[name="openFlag"]:checked').val();
        if (!CheckUtil.isNotNull(openFlag)) {
            layer.msg("请选择是否开启!");
            return;
        }
        if ($("#isReCommit").val() == 0) {
            $("#isReCommit").val(1);
            //loading层
            var index = layer.load(0, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $("#gameId").val($("#gameIdSelect").val());
            $("#gameName").val($("#gameIdSelect option:selected").text());
            $.ajax({
                url: action[opera],
                type: "post",
                cache: false,
                //data:parseJson(decodeURIComponent($("#saveAndUpdateFrom").serialize())),
                data: new FormData($("#saveAndUpdateFrom")[0]),
                dataType:"json",
                processData: false,
                contentType: false,
                async:false,
                success: function(data) {
                    layer.close(index);
                    if(data.code == 0){
                        parent.layer.msg("添加成功！");
                    } else if(data.code == 1){
                        parent.layer.msg("修改成功！");
                    } else if(data.code == "-2"){
                        parent.window.location.href="${sysManagerV1}/";
                    } else {
                        parent.layer.msg(data.message);
                    }
                    if(data.code >= 0){
                        parent.findData();
                        parent.layer.close(parent.tableIndex);
                    }
                }
            });
        }
    }
</script>
</html>